<?php
/**
* @file $Id: Schools.php 430 2007-02-12 23:54:21Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

unset($_SESSION['_REQUEST_vars']['values']);unset($_SESSION['_REQUEST_vars']['modfunc']);
DrawHeader(ProgramTitle());
echo '<BR>';
if($_REQUEST['modfunc']=='update' && $_POST['button']==_('Save') && AllowEdit())
{
	if($_REQUEST['values']['PHONE'])
		$_REQUEST['values']['PHONE'] = ereg_replace('[^0-9]+','',$_REQUEST['values']['PHONE']);

	if($_REQUEST['values'] && $_POST['values'] && User('PROFILE')=='admin')
	{
		if($_REQUEST['new_school']!='true')
		{
			$sql = "UPDATE SCHOOLS SET ";
							
			foreach($_REQUEST['values'] as $column=>$value)
			{
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE ID='".UserSchool()."'";
			DBQuery($sql);
			echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';
			$note[] = _('This school has been modified.');
		}
		else
		{
			$sql = "INSERT INTO SCHOOLS ";

			$fields = 'ID,';
			$values = db_seq_nextval('SCHOOLS_SEQ').",";

			$go = 0;
			unset($_REQUEST['values']['ID']);
			foreach($_REQUEST['values'] as $column=>$value)
			{
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
			
			if($go)
			{
				DBQuery($sql);
				echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';
			}
		}
	}

	$_REQUEST['modfunc'] = '';
	unset($_SESSION['_REQUEST_vars']['values']);
	unset($_SESSION['_REQUEST_vars']['modfunc']);
}

if($_REQUEST['modfunc']=='update' && $_REQUEST['button']==_('Delete') && User('PROFILE')=='admin' && AllowEdit())
{
	if(DeletePrompt(_('school')) && AllowEdit())
	{
		DBQuery("DELETE FROM SCHOOLS WHERE ID='".UserSchool()."'");
		DBQuery("DELETE FROM SCHOOL_GRADELEVELS WHERE SCHOOL_ID='".UserSchool()."'");
		DBQuery("DELETE FROM ATTENDANCE_CALENDAR WHERE SCHOOL_ID='".UserSchool()."'");
		DBQuery("DELETE FROM SCHOOL_PERIODS WHERE SCHOOL_ID='".UserSchool()."'");
		DBQuery("DELETE FROM SCHOOL_SEMESTERS WHERE SCHOOL_ID='".UserSchool()."'");
		DBQuery("DELETE FROM SCHOOL_QUARTERS WHERE SCHOOL_ID='".UserSchool()."'");
		DBQuery("DELETE FROM SCHOOL_PROGRESS_PERIODS WHERE SCHOOL_ID='".UserSchool()."'");
		DBQuery("UPDATE USERS SET CURRENT_SCHOOL_ID=NULL WHERE CURRENT_SCHOOL_ID='".UserSchool()."'");
		DBQuery("UPDATE USERS SET SCHOOLS=replace(SCHOOLS,',".UserSchool().",','')");

		unset($_SESSION['UserSchool']);
		echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';
		unset($_REQUEST);
		$_REQUEST['modname'] = "School_Setup/Schools.php?new_school=true";
		$_REQUEST['new_school'] = true;
		unset($_REQUEST['modfunc']);
	}
}

if(!$_REQUEST['modfunc'])
{
	if(!$_REQUEST['new_school'])
	{
		$schooldata = DBGet(DBQuery("SELECT ID,TITLE,ADDRESS,CITY,STATE,ZIPCODE,AREA_CODE,PHONE,PRINCIPAL FROM SCHOOLS WHERE ID='".UserSchool()."' "));
		$school_name = GetSchool(UserSchool());
	}
	else
		$school_name = _('Add a School');

	echo "<FORM METHOD='POST' ACTION='Modules.php?modname=".$_REQUEST['modname']."&modfunc=update&new_school=$_REQUEST[new_school]'>";
	PopTable('header',$school_name);
	echo "<TABLE>";
	
	if($_REQUEST['new_school']=='true')
	{
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('School Name')."</TD>";
		echo "<TD><INPUT NAME='values[TITLE]' TYPE=TEXT VALUE='".$schooldata[1]['TITLE']."' size=40 maxlength=100>";
		echo "</TD></TR>";

		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('Address')."</TD>";
		echo "<TD><INPUT NAME='values[ADDRESS]' TYPE=TEXT SIZE=40 MAXLENGTH=100 VALUE='".$schooldata[1]['ADDRESS']."'>";
		echo "</TD></TR>";
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('City / State')."</TD>";
		echo "<TD><INPUT NAME='values[CITY]' TYPE=TEXT SIZE=35 MAXLENGTH=100 VALUE='".$schooldata[1]['CITY']."'> <INPUT NAME='values[STATE]' TYPE=TEXT SIZE=3 MAXLENGTH=2 VALUE='".$schooldata[1]['STATE']."'>";
		echo "</TD></TR>";
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('Zip Code')."</TD>";
		echo "<TD><INPUT NAME='values[ZIPCODE]' TYPE=TEXT SIZE=6 VALUE='".$schooldata[1]['ZIPCODE']."'>";
		echo "</TD></TR>";
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('Phone')."</TD>";
		echo "<TD>(<INPUT NAME='values[AREA_CODE]' TYPE=TEXT SIZE=4 MAXLENGTH=3 VALUE='".$schooldata[1]['AREA_CODE']."'>) ";
		echo "<INPUT NAME='values[PHONE]' TYPE=TEXT SIZE=9 MAXLENGTH=8 VALUE='".substr($schooldata[1]['PHONE'],0,3)."'>";
		echo "</TD></TR>";
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('Principal')."</TD>";
		echo "<TD><INPUT NAME='values[PRINCIPAL]' TYPE=TEXT SIZE=40 MAXLENGTH=100 VALUE='".$schooldata[1]['PRINCIPAL']."'>";
		echo "</TD></TR>";
	}
	else
	{
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('School Name')."</TD><TD>".TextInput($schooldata[1]['TITLE'],'values[TITLE]','','maxlength=100')."</TD></TR>";
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('Address')."</TD><TD>".TextInput($schooldata[1]['ADDRESS'],'values[ADDRESS]','','maxlength=100')."</TD></TR>";
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('City / State')."</TD>";
		echo "<TD><TABLE cellspacing=0 cellpadding=0><TR><TD>".TextInput($schooldata[1]['CITY'],'values[CITY]','','maxlength=100').'</TD><TD>, &nbsp;</TD><TD>'.TextInput($schooldata[1]['STATE'],'values[STATE]','','maxlength=2').'</TD></TR></TABLE></TD></TR>';
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('Zip Code')."</TD><TD>".TextInput($schooldata[1]['ZIPCODE'],'values[ZIPCODE]','','')."</TD></TR>";
		
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('Phone')."</TD>";
		echo "<TD><TABLE cellspacing=0 cellpadding=0><TR><TD>(</TD><TD>".TextInput($schooldata[1]['AREA_CODE'],'values[AREA_CODE]','','maxlength=3')."</TD><TD>) &nbsp;</TD><TD>";
		echo TextInput(substr($schooldata[1]['PHONE'],0,3).($schooldata[1]['PHONE']?'-':'').substr($schooldata[1]['PHONE'],3),'values[PHONE]','','maxlength=8');
		echo "</TD></TR></TABLE></TD></TR>";
		
		echo "<TR VALIGN=TOP align=".ALIGN_LEFT."><TD>"._('Principal')."</TD><TD>".TextInput($schooldata[1]['PRINCIPAL'],'values[PRINCIPAL]','','maxlength=100')."</TD></TR>";
	}
	echo "</TABLE>";
	if(User('PROFILE')=='admin' && AllowEdit())
		echo "<CENTER><INPUT TYPE=SUBMIT name=button VALUE='"._('Save')."'>".(($_REQUEST['new_school']!='true')?"<INPUT type=submit name=button value="._('Delete').">":'')."</CENTER>";
	PopTable('footer');
	echo "</FORM>";

}

?>